package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/label")
@CrossOrigin //开启跨域访问支持
public class LabelController {

    @Autowired
    private LabelService labelService;

    //查询所有
    @RequestMapping(method = RequestMethod.GET)
    public Result list(){
        List<Label> list = labelService.findAll();
        return new Result(StatusCode.OK, true, "查询成功",list);
    }

    //根据id查询
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable String labelId){
//        int i = 10/0;
        Label label = labelService.findById(labelId);
        return new Result(StatusCode.OK, true, "查询成功",label);
    }

    //保存
    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
        labelService.save(label);
        return new Result(StatusCode.OK, true, "添加成功");
    }

    //根据id修改
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@RequestBody Label label,@PathVariable String id){
        label.setId(id);
        labelService.update(label);
        return new Result(StatusCode.OK, true, "修改成功");
    }

    //根据id删除
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id){
        labelService.delete(id);
        return new Result(StatusCode.OK, true, "删除成功");
    }
}
